﻿using Ninject;
using System.Linq;
using System.Web.Mvc;
using Peninsula.Account.Manager;
using Peninsula.Web.Infrastructure.Constant;
using Peninsula.Web.Models.Sessions;

namespace Peninsula.Web.Infrastructure
{

    [AdminAuthorize]
    public class AdminBaseController : CommonBaseController
    {
        [Inject]
        public AdminManager AdminManager { get; set; }

        [Inject]
        public PermissionManager PermissionManager { get; set; }

        [Inject]
        public RoleManager RoleManager { get; set; }
        [Inject]
        public HouseholdsManger Householdsmanger { get; set;} 

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            if (!(new string[] { })
                .Contains(filterContext.RequestContext.RouteData.Values["controller"].ToString().ToLower())
               && filterContext.Result.GetType() == typeof(ViewResult))
            {
                ViewBag.AdminSession = AdminSession;
            }
        }

        #region 管理员SESSION相关

        /// <summary>
        /// 获取管理员session数据
        /// </summary>
        public AdminSession AdminSession
        {
            get
            {
                return Session[SessionConstant.ADMIN_SESSION] as AdminSession;
            }
            set
            {
                Session[SessionConstant.ADMIN_SESSION] = value;
            }
        }

        /// <summary>
        /// 管理员登录
        /// </summary>
        protected void AdminSignIn(AdminSession adminSession, bool rememberMe)
        {
            FormsAuthenticationHelper.SignIn(adminSession.Identity, rememberMe);
            AdminSession = adminSession;
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        protected void AdminSignOut()
        {
            FormsAuthenticationHelper.SignOut();
            Session.Abandon();
        }
        
        #endregion

        #region 辅助Redirect方法

        [NonAction]
        public override ActionResult RedirectToMessage(string message = null, string title = null, string returnUrl = null, int code = 500)
        {
            TempData["IsRedirect"] = true;
            TempData["Message"] = message;
            TempData["Title"] = title;
            TempData["ReturnUrl"] = returnUrl ?? Request.UrlReferrer?.ToString();

            return RedirectToRoute(RouteConfig.AdminDefault, new { action = "Error", controller = "Errors", RedirectUrl = returnUrl, code = code });
        }

        /// <summary>
        /// 重定向到公共错误页面
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="currentPage">引发错误的页面名称</param>
        /// <param name="returnUrl">重定向地址</param>
        [NonAction]
        public override ActionResult RedirectToError(string message = null, string currentPage = null, string returnUrl = null, int code = 500)
        {
            TempData["IsRedirect"] = true;
            TempData["Message"] = message;
            TempData["CurrentPage"] = currentPage;
            TempData["ReturnUrl"] = returnUrl ?? Request.UrlReferrer?.ToString();

            return RedirectToRoute(RouteConfig.AdminDefault, new { action = "Error", controller = "Errors", code = code, RedirectUrl = returnUrl });
        }

        #endregion

    }
}